package com.flower.subject.domain.handler.subject;

import com.flower.subject.domain.convent.JudgeSubjectConverter;
import com.flower.subject.domain.convent.MultipleSubjectConverter;
import com.flower.subject.domain.entity.SubjectAnswerBO;
import com.flower.subject.domain.entity.SubjectInfoBO;
import com.flower.subject.domain.entity.SubjectOptionBO;
import com.flower.subject.enums.SubjectInfoTypeEnum;
import com.flower.subject.infra.basic.entity.SubjectBrief;
import com.flower.subject.infra.basic.entity.SubjectJudge;
import com.flower.subject.infra.basic.entity.SubjectMultiple;
import com.flower.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-08 14:48
 * 多选
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService service;
    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectMultiple> subjectJudges = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(item -> {
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANT.convertBoToEntity(item);
            subjectMultiple.setSubjectId(subjectInfoBO.getId().intValue());
            subjectJudges.add(subjectMultiple);
        });

        service.batchInsert(subjectJudges);

    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple>  subjectMultiples = service.queryByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOS = MultipleSubjectConverter.INSTANT.covertAnswerBoToBOList(subjectMultiples);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
